package com.cristina.attention.rest;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.cristina.attention.entity.GroupMember;
import com.cristina.attention.service.GroupMemberService;
import com.cristina.attention.mp.support.Condition;
import com.cristina.attention.utils.Func;
import com.cristina.attention.utils.R;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

/**
 * @author by superman
 * @Date 2021/12/9
 */
@Slf4j
@RestController
@RequestMapping("core/groupMember")
@AllArgsConstructor
public class GroupMemberController {

    private final GroupMemberService groupMemberService;

    /**
     * 查询单条
     */
    @ApiOperation(value = "查看详情", notes = "传入id")
    @GetMapping("/detail")
    public R<Object> detail(GroupMember groupMember) {
        GroupMember detail = groupMemberService.getOne(Condition.getQueryWrapper(groupMember));
        return R.data(detail);
    }

    /**
     * 查询单条
     */
    @ApiOperation(value = "查看详情", notes = "传入id")
    @GetMapping("/info")
    public R<Object> info(GroupMember groupMember) {
        GroupMember detail = groupMemberService.getById(groupMember.getId());
        return R.data(detail);
    }

    @GetMapping("/list")
    @ApiOperation(value = "列表")
    public R<Object> list() {
        return R.data(groupMemberService.list());
    }

    /**
     * 新增或修改
     */
    @ApiOperation(value = "新增或修改", notes = "传入groupMember")
    @PostMapping("/submit")
    public R<Object> submit(@RequestBody GroupMember groupMember) {
        return R.status(groupMemberService.save(groupMember));
    }

    /**
     * 修改
     */
    @ApiOperation(value = "修改", notes = "传入groupMember")
    @PostMapping("/update")
    public R<Object> update(@RequestBody GroupMember groupMember) {
        return R.status(groupMemberService.updateById(groupMember));
    }

    /**
     * 删除
     */
    @ApiOperation(value = "删除", notes = "传入messageId")
    @PostMapping("/remove")
    public R<Object> remove(@PathVariable String messageId) {
        return R.status(groupMemberService.deleteLogic(Func.toLongList(messageId)));
    }

    /**
     * 查询
     * @param userId
     * @return
     */
    @ApiOperation(value = "查询用户所在的所有群聊", notes = "传入userId")
    @GetMapping("/findGroupBelonging/{userId}")
    public R<Object> findGroupBelonging(@PathVariable Long userId) {
        return R.data(groupMemberService.findGroupBelonging(userId));
    }

    @ApiOperation(value = "根据群id 获取群成员", notes = "传入groupId")
    @GetMapping("/findGroupMemberByGroupId")
    public R<Object> findGroupMemberByGroupId(@RequestParam Long groupId, @RequestParam Long userId) {
        return R.data(groupMemberService.findGroupMemberByGroupId(groupId, userId));
    }

}
